//
//  EMSMSCodeView.m
//  EasyMoney
//
//  Created by 邓乐 on 2020/1/4.
//  Copyright © 2020 zh. All rights reserved.
//

#import "EMSMSCodeView.h"

@interface EMSMSCodeView ()

@property (nonatomic, strong) YYTimer *timer;
@property (nonatomic, assign) NSInteger duration;
@property (nonatomic, assign) NSInteger durationTime;
@property (nonatomic, assign) double beginTime;

@end

@implementation EMSMSCodeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        UIImage *normalImage = [UIImage gradientImageSize:CGSizeMake(280, 44) colors:@[ColorFromRGB(0x36B2FF),ColorFromRGB(0x2F5AFF)] startPoint:CGPointMake(0, 22) endPoint:CGPointMake(280, 22)];
//
//        UIImage *disableImage = [UIImage imageWithColor:[UIColor colorWithRGB:0x2761ff alpha:0.5]];
        
//        [self setBackgroundImage:normalImage forState:UIControlStateNormal];
//        [self setBackgroundImage:disableImage forState:UIControlStateDisabled];
        self.clipsToBounds = YES;
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI
{
    self.smsBtn = [[UIButton alloc] init];
    self.smsBtn.layer.cornerRadius = 18;
    self.smsBtn.layer.masksToBounds = YES;
    self.smsBtn.titleLabel.font = FONT_PINGFANG_REGULAR_(13);
    UIImage *normalImage = [UIImage imageWithColor:ColorFromRGBWithAlpha(0x2F5AFF,0.16)];
    [self.smsBtn setTitleColor:ColorFromRGB(0x2F5AFF) forState:UIControlStateNormal];
    [self.smsBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
    UIImage *disableImage = [UIImage imageWithColor:[UIColor colorWithRGB:0xEEEEEE alpha:1.0]];
    [self.smsBtn setTitleColor:ColorFromRGB(0x999999) forState:UIControlStateDisabled];
    [self.smsBtn setBackgroundImage:disableImage forState:UIControlStateDisabled];
    self.smsBtn.titleLabel.font = FONT_PINGFANG_REGULAR_(13);
    [self.smsBtn setTitle:NSLocalizedString(@"获取验证码", nil) forState:UIControlStateNormal];
    [self.smsBtn addTarget:self action:@selector(clickSmsBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.smsBtn];
    [self.smsBtn setEnabled:NO];
    [self.smsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(140, 36));
        make.edges.equalTo(self);
    }];
}

- (void)clickSmsBtn:(UIButton *)sender
{
    [self.delegate smsCodeView:self clickSmsBtn:sender];
}

- (void)startTimerWithDuration:(NSInteger)time
{
    self.beginTime = [NSDate getCPUTime];
    self.durationTime = time;
    self.duration = self.durationTime;
    self.timer = [YYTimer timerWithTimeInterval:1 target:self selector:@selector(authCodeTimerAction) repeats:YES];
}

- (void)authCodeTimerAction
{
    if (self.duration <= 0) {
        self.duration = 0;
        
        [self.smsBtn setTitle:NSLocalizedString(@"获取验证码", nil) forState:UIControlStateDisabled];
        [self.smsBtn setTitle:NSLocalizedString(@"获取验证码", nil) forState:UIControlStateNormal];
        [self.smsBtn setEnabled:YES];
        
        if (self.timer) {
            [self.timer invalidate];
            self.timer = nil;
        }
    } else {
        NSString* noteStr = [NSString stringWithFormat:NSLocalizedString(@"%lds获取验证码", nil),(long)self.duration];
        [self.smsBtn setTitle:noteStr forState:UIControlStateDisabled];
        self.duration = self.durationTime - (int)round([NSDate getCPUTime] - self.beginTime);
    }
}

@end
